package com.test.mylotterysystem.comment.utils;

import com.test.mylotterysystem.comment.errorCode.ControllerErrorCode;
import com.test.mylotterysystem.comment.exception.ControllerException;
import com.test.mylotterysystem.controller.Param.PrizeInfo;
import com.test.mylotterysystem.controller.Result.*;
import com.test.mylotterysystem.dao.DO.PrizeDO;
import com.test.mylotterysystem.dao.DO.UserDO;
import com.test.mylotterysystem.service.DTO.*;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class MyBeanUtil {
    public static UserRegisterResult transOfUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        if (userRegisterDTO == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }
        UserRegisterResult userRegisterResult = new UserRegisterResult();
        BeanUtils.copyProperties(userRegisterDTO, userRegisterResult);
        return userRegisterResult;
    }

    public static UserLoginResult transOfUserLoginResult(UserLoginDTO userLoginDTO) {
        if (userLoginDTO == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        BeanUtils.copyProperties(userLoginDTO, userLoginResult);
        return userLoginResult;
    }

    public static List<UserInfo> transOfListUserInfo(List<UserInfoDTO> userInfoDTOS) {
        if (userInfoDTOS == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }
        return userInfoDTOS.stream().map(userInfoDTO -> {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userInfoDTO.getId());
            userInfo.setUserName(userInfoDTO.getUserName());
            userInfo.setIdentity(userInfoDTO.getIdentity());
            return userInfo;
        }).collect(Collectors.toList());
    }

    public static List<UserInfoDTO> transOfListUserInfoDTO(List<UserDO> userDOS) {
        if (userDOS == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }

        return userDOS.stream().map(userDO -> {
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userDO, userInfoDTO);
            return userInfoDTO;
        }).collect(Collectors.toList());
    }

    public static PrizeListResult transOfPrizeListResult(PrizeListResultDTO<PrizeInfo> prizeListResultDTO) {
        if (prizeListResultDTO == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }

        PrizeListResult prizeListResult = new PrizeListResult();
        prizeListResult.setTotal(prizeListResultDTO.getTotal());
        prizeListResult.setRecords(prizeListResultDTO.getRecords());
        return prizeListResult;

    }

    public static List<PrizeInfo> transOfPrizeListResultDTO(List<PrizeDO> prizeDOS) {
        if (prizeDOS == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }

        return prizeDOS.stream().map(prizeDO -> {
            PrizeInfo prizeInfo = new PrizeInfo();
            prizeInfo.setPrizeId(prizeDO.getId());
            prizeInfo.setPrizeName(prizeDO.getName());
            prizeInfo.setDescription(prizeDO.getDescription());
            prizeInfo.setPrice(prizeDO.getPrice());
            prizeInfo.setImageUrl(prizeDO.getImageUrl());
            return prizeInfo;
        }).collect(Collectors.toList());
    }

    public static ActivityCreateResult transOfActivityCreateResult(ActivityCreateResultDTO activityCreateResultDTO) {
        if (activityCreateResultDTO == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }
        ActivityCreateResult activityCreateResult = new ActivityCreateResult();
        activityCreateResult.setActivityId(activityCreateResultDTO.getActivityId());
        return activityCreateResult;
    }

    public static GetActivityResult transOfGetActivityResult(ActivityListResultDTO<ActivityResultDTO> activityListResultDTO) {
        if (activityListResultDTO == null) {
            throw new ControllerException(ControllerErrorCode.TRANS_IS_ERROR);
        }

        GetActivityResult getActivityResult = new GetActivityResult();
        getActivityResult.setTotal(activityListResultDTO.getTotal());

        List<GetActivityResult.ActivityInfo> ret = activityListResultDTO.getRecords().stream().map(activityResultDTO -> {
            GetActivityResult.ActivityInfo activityInfo = new GetActivityResult.ActivityInfo();
            activityInfo.setActivityId(activityResultDTO.getActivityId());
            activityInfo.setActivityName(activityResultDTO.getActivityName());
            activityInfo.setDescription(activityResultDTO.getDescription());
            activityInfo.setValid(activityResultDTO.vaild());
            return activityInfo;
        }).collect(Collectors.toList());

        getActivityResult.setRecords(ret);
        return getActivityResult;
    }
}
